<?php
//
// Article Class
// The SDMworld! Network
//

uiLoadLibrary('page');

class clsForum extends cDycmsObject
{
    protected $mClsId = 'Forum';

	function Render(cDycmsClientNode &$rClient, $disp = true)
	{
		//
		// Query all top level threads in this forum
		//
		
		$obj = new cDycmsObjectQuery('ForumPost');
		$obj->AddArray(array(
			'forum' => $this->mId,
			'replyTo' => 0));
		$n = $obj->Execute();
		
		$rClient->AddChild($hdr = new cUiLayer(array('class' => 'ContentTitle')));
		$hdr->AddChild($this->mCoreAttributes['title']);
		
		
		//
		// Create the 'New Topic' button
		//
		
		$btn = new cUiForm(array(
			'class' => 'ContentBody',
			'action' => dyEditorUrl('ForumPost'),
			'method' => 'POST',
			'id' => 'Forum-'.$this->mId));
		$btn->AddChild(new cUiButton(array(
			'name' => 'forum',
			'value' => $this->mId
		), 'New Topic', 'page_add'));
		
		//
		// Create the table
		//
		
		$rClient->AddChild($btn);
		$rClient->AddChild($tbl = new cUiTable(array(
			'Topic', 'Author', 'Replies', 'Views', 'Most Recent Post'), array(
			'class' => 'Forum'
		)));
		$rClient->AddChild($btn);
		
		for($i = 0; $i < $n; $i++){
			$tbl->AddChild($row = new cUiTableRow());
			
			$row->AddChild(new cHtmlHyperlink(
				dyViewerUrl('ForumPost', $obj->GetId($i)),
				$obj->GetSummary($i)));
			
			$row->AddChild($obj->GetOwner($i)->GetUserName());
			$row->AddChild("0");
			$row->AddChild("0");
			$row->AddChild("");
		}
	}

	function DoEdit($vzAttrib){
		
		//
		// Set the title and site, for publishing
		//
		
		if(isset($vzAttrib['$$Forum-title']))
			$this->mCoreAttributes['title'] = $vzAttrib['$$Forum-title'];
		if(isset($vzAttrib['$$Forum-site']))
			$this->mCoreAttributes['site'] = $vzAttrib['$$Forum-site'];
		
		parent::DoEdit($vzAttrib);
	}
	
	function Write(){
		parent::Write();
		return $this->mClass->SetIndexValues($this->mId, $this->mCoreAttributes);
	}
	
	function DrawEditor(cUiEditor &$editor)
	{
		//
		// Draw the editor form
		//

		$editor->AddChild(new cUiTextBox(array(
			'name' => '$$Forum-title',
			'value' => $this->mCoreAttributes['title']
		), 'Title'));
		$editor->AddChild(new cUiTextBox(array(
			'name' => '$$Forum-site',
			'value' => $this->mCoreAttributes['site']
		), 'Site'));
		$editor->AddChild(new cUiEditorTextArea('description',
			$this->mAttributes['description'], 'Description'));
		
		//
		// Add buttons
		//
		
		$editor->AddChild(new cUiButtonSet(array(
			'name' => '$$os-editorAction',
			'method' => 'POST',
			'action' => ''), $this->mId == 0 ? array(
				'save' => uiGetIcon('disk').'Save'
			) : array(
				'save' => uiGetIcon('disk').'Save Changes',
				'delete' => uiGetIcon('delete').'Delete'
		)));
	}
	
	function DrawPublisher(cUiPublisher &$rPublisher)
	{
		throw new dyExInvalidOp('Forums can only be published via the editor');
	}
	
	function GetSummary()
	{
        if($this->mCoreAttributes['title'] === null)
            return 'Unpublished Forum';
        else
            return $this->mCoreAttributes['title'];
	}

	function LoadObject()
	{
        $this->mCoreAttributes = $this->mClass->GetIndexValues($this->mId);
	}

    function Create(){
        $this->mAttributes['description'] = '';
		$this->mCoreAttributes['title'] = '';
		$this->mCoreAttributes['site'] = '';
    }
}

class cCollectionForum extends cDycmsAbstractCollection
{
	function Render(cDycmsClientNode &$rClient)
	{
		$rClient->AddChild($tbl = new cUiTable(array(
			'Forum', 'Threads', 'Posts', 'Most Recent')));
		
		foreach($this->mzObject as $rForum){
			$tbl->AddChild($row = new cUiTableRow());
			
			$main = new cDycmsClientNode();
			$main->AddChild(new cHtmlHyperlink(
				dyViewerUrl('Forum', $rForum->GetId()),
				$rForum->GetSummary()));
			$main->AddChild('<br/>'.$rForum->Get('description'));
			
			$row->AddChild($main);
			$row->AddChild("0");
			$row->AddChild("0");
			$row->AddChild("");
		}
	}

	public function GetSummary($i = -1)
	{
		return "The Forums";
	}
}

?>
